<?php
class AjaxController extends AdminAppController{
	
    function convert_link()
    {
        $this->autoRender = false;
        $content = $_POST['content'];
        return $this->unicode_convert($content);
        
    }
    function change_status_category()
	{
		$this->loadModel('Admin.Category');
		$category_id = $_POST['category_id'];
		echo $this->Category->change_status($category_id);
		$this->autoRender = false;
	}
    
    function delete_category()
	{
		$this->loadModel('Admin.Category');
		$category_id = $_POST['category_id'];
		$this->Category->del($category_id);
        $this->Session->setFlash(
                                'Xóa danh mục thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
		$this->autoRender = false;
	}

	function change_status_article()
	{
		$this->loadModel('Admin.Article');
		$article_id = $_POST['article_id'];
		echo $this->Article->change_status($article_id);
		$this->autoRender = false;
	}
    
    function delete_article()
	{
		$this->loadModel('Admin.Article');
		$article_id = $_POST['article_id'];
		$this->Article->del($article_id);
        $this->Session->setFlash(
                                'Xóa bài viết thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
		$this->autoRender = false;
	}
    
    function change_status_user()
	{
		$this->loadModel('Admin.User');
		$user_id = $_POST['user_id'];
		echo $this->User->change_status($user_id);
		$this->autoRender = false;
	}
    
    function delete_user()
	{
		$this->loadModel('Admin.User');
		$user_id = $_POST['user_id'];
		$this->User->del($user_id);
		$this->autoRender = false;
	}
    
    function delete_compo()
	{
		$this->loadModel('Admin.Composition');
		$compo_id = $_POST['compo_id'];
		$this->Composition->del($compo_id);
        $this->Session->setFlash(
                                'Xóa thành phần thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
		$this->autoRender = false;
	}
    
    function change_status_compo()
    {
        $this->loadModel('Admin.Composition');
		$compo_id = $_POST['compo_id'];
		echo $this->Composition->change_status($compo_id);
		$this->autoRender = false;
    }
    
    function delete_brand()
	{
		$this->loadModel('Admin.Brand');
		$brand_id = $_POST['brand_id'];
		$this->Brand->del($brand_id);
        $this->Session->setFlash(
                                'Xóa thành phần thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
		$this->autoRender = false;
	}
    
    function change_status_brand()
    {
        $this->loadModel('Admin.Brand');
		$brand_id = $_POST['brand_id'];
		echo $this->Brand->change_status($brand_id);
		$this->autoRender = false;
    }
    
    function change_status_product()
    {
        $this->loadModel('Admin.Product');
		$product_id = $_POST['product_id'];
		echo $this->Product->change_status($product_id);
		$this->autoRender = false;
    }
    
    function delete_product()
    {
        $this->loadModel('Admin.Product');
		$product_id = $_POST['product_id'];
		$this->Product->del($product_id);
        $this->Session->setFlash(
                                'Xóa sản phẩm thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
		$this->autoRender = false;
    }
    
    function get_status_cart()
    {
        $this->loadModel('Admin.Cart');
        $cart_id = $_POST['cart_id'];
		echo $this->Cart->get_status_cart($cart_id);
		$this->autoRender = false;
    }
    
    function change_status_cart()
    {
        $color = array('yellow', 'blue', 'green', 'red');
        $this->loadModel('Admin.Cart');
        $cart_id = $_POST['cart_id'];
        $status = $_POST['status'];
        $title = $this->Cart->change_status_cart($cart_id, $status);
        $result['title'] = $title;
        $result['color'] = $color[$status];
        echo json_encode($result); 
        $this->autoRender = false;
    }
    
    function delete_cart()
    {
        $this->loadModel('Admin.Cart');
		$cart_id = $_POST['cart_id'];
		$this->Cart->del($cart_id);
        $this->Session->setFlash(
                                'Xóa giỏ hàng thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
		$this->autoRender = false;
    }
    
    function delete_newsletter()
    {
        $this->loadModel('Admin.Newsletter');
		$newsletter_id= $_POST['newsletter_id'];
		$this->Newsletter->delete($newsletter_id);
        $this->Session->setFlash(
                                'Xóa email thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
		$this->autoRender = false;
    }
    
    function delete_menu()
    {
        $this->loadModel('Admin.Menu');
		$menu_id = $_POST['menu_id'];
		$this->Menu->del($menu_id);
        $this->Session->setFlash(
                                'Xóa menu thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
		$this->autoRender = false;
    }
    
    function change_status_menu()
    {
        $this->loadModel('Admin.Menu');
		$menu_id = $_POST['menu_id'];
		echo $this->Menu->change_status($menu_id);
		$this->autoRender = false;
    }
    
    function delete_media()
    {
        $this->loadModel('Admin.Media');
		$media_id = $_POST['media_id'];
		$this->Media->del($media_id);
        $this->Session->setFlash(
                                'Xóa media thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
		$this->autoRender = false;
    }
    
    function change_status_media()
    {
        $this->loadModel('Admin.Media');
		$media_id = $_POST['media_id'];
		echo $this->Media->change_status($media_id);
		$this->autoRender = false;
    }
    
    function unicode_convert($str){
    	if(!$str) return false;
    	$unicode = array(
    				'a'=>array('á','à','ả','ã','ạ','ă','ắ','ặ','ằ','ẳ','ẵ','â','ấ','ầ','ẩ','ẫ','ậ'),
    				'A'=>array('Á','À','Ả','Ã','Ạ','Ă','Ắ','Ặ','Ằ','Ẳ','Ẵ','Â','Ấ','Ầ','Ẩ','Ẫ','Ậ'),
    				'd'=>array('đ'),
    				'D'=>array('Đ'),
    				'e'=>array('é','è','ẻ','ẽ','ẹ','ê','ế','ề','ể','ễ','ệ'),
    				'E'=>array('É','È','Ẻ','Ẽ','Ẹ','Ê','Ế','Ề','Ể','Ễ','Ệ'),
    				'i'=>array('í','ì','ỉ','ĩ','ị'),
    				'I'=>array('Í','Ì','Ỉ','Ĩ','Ị'),
    				'o'=>array('ó','ò','ỏ','õ','ọ','ô','ố','ồ','ổ','ỗ','ộ','õ','ớ','ờ','ở','ỡ','ợ'),
    				'O'=>array('Ó','Ò','Ỏ','Õ','Ọ','Ô','Ố','Ồ','Ổ','Ỗ','Ộ','Õ','Ớ','Ờ','Ở','Ỡ','Ợ'),
    				'u'=>array('ú','ù','ủ','ũ','ụ','ý','ứ','ừ','ử','ữ','ự'),
    				'U'=>array('Ú','Ù','Ủ','Ũ','Ụ','Ý','Ứ','Ừ','Ử','Ữ','Ự'),
    				'y'=>array('ý','ỳ','ỷ','ỹ','ỵ'),
    				'Y'=>array('Ý','Ỳ','Ỷ','Ỹ','Ỵ'),
    				'-'=>array(' ','.','/',"'",'`','(',')',',','!','"','"','"','%','&','@','#','$'),
    				);
    	foreach($unicode as $nonUnicode=>$uni){
    		foreach($uni as $value)
    		$str = str_replace($value,$nonUnicode,$str);
    	}
        $str = Inflector::slug($str, '-');
    	$str = strtolower($str);
    	return $str;
    }
    
    function delete_translate()
	{
		$this->loadModel('Admin.Translate');
		$translate_id = $_POST['translate_id'];
		$this->Translate->del($translate_id);
        $this->Session->setFlash(
                                'Xóa thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
		$this->autoRender = false;
	}
}